{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T08:58:58.183942Z",
     "start_time": "2025-01-08T08:58:57.967679Z"
    }
   },
   "source": [
    "# 导入库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import random"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:00:32.969693Z",
     "start_time": "2025-01-08T09:00:32.963374Z"
    }
   },
   "cell_type": "code",
   "source": [
    "index1 = pd.MultiIndex.from_arrays([['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'],\n",
    "                                    [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]], names=['cloth', 'size'])\n",
    "\n",
    "ser_obj = pd.Series(np.random.randn(12), index=index1)\n",
    "\n",
    "#unstack将多索引转为列索引\n",
    "df_obj = ser_obj.unstack(0)\n",
    "print(df_obj)\n",
    "\n",
    "#计算最小值有空值的列,不考虑nan\n",
    "df_obj.loc[0, 'b'] = np.nan\n",
    "print(df_obj.min(axis=0))"
   ],
   "id": "17edc416acf489bb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth         a         b         c         d\n",
      "size                                         \n",
      "0     -0.110558  0.723999  0.224148 -1.181987\n",
      "1      1.090637  0.248019  0.257323  1.316145\n",
      "2      0.801136 -0.773192  0.771602 -0.526025\n",
      "cloth\n",
      "a   -0.110558\n",
      "b   -0.773192\n",
      "c    0.224148\n",
      "d   -1.181987\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:01:32.774652Z",
     "start_time": "2025-01-08T09:01:32.767794Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 描述性统计，数据分布情况\n",
    "print(df_obj.describe())"
   ],
   "id": "26d9c064ac8cf1e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth         a         b         c         d\n",
      "count  3.000000  2.000000  3.000000  3.000000\n",
      "mean   0.593739 -0.262586  0.417691 -0.130622\n",
      "std    0.626879  0.722105  0.306945  1.295154\n",
      "min   -0.110558 -0.773192  0.224148 -1.181987\n",
      "25%    0.345289 -0.517889  0.240735 -0.854006\n",
      "50%    0.801136 -0.262586  0.257323 -0.526025\n",
      "75%    0.945887 -0.007284  0.514463  0.395060\n",
      "max    1.090637  0.248019  0.771602  1.316145\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:03:24.590330Z",
     "start_time": "2025-01-08T09:03:24.582876Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#计算最小值或者最大值的索引的位置\n",
    "\n",
    "# argmin()返回最小值索引的位置，argmax()返回最大值索引的位置\n",
    "print(df_obj.idxmin())\n",
    "print(df_obj.idxmax())\n",
    "\n",
    "# idxmin()返回最小值索引，idxmax()返回最大值索引\n",
    "# skipna=False，返回nan的索引\n",
    "print(df_obj.idxmin(axis=0, skipna=True))\n",
    "print(df_obj.idxmax(axis=0, skipna=True))"
   ],
   "id": "200bf3c66ec9ade7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cloth\n",
      "a    0\n",
      "b    2\n",
      "c    0\n",
      "d    0\n",
      "dtype: int64\n",
      "cloth\n",
      "a    1\n",
      "b    1\n",
      "c    2\n",
      "d    1\n",
      "dtype: int64\n",
      "cloth\n",
      "a    0\n",
      "b    2\n",
      "c    0\n",
      "d    0\n",
      "dtype: int64\n",
      "cloth\n",
      "a    1\n",
      "b    1\n",
      "c    2\n",
      "d    1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "2a1271b37c74618a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
